<?php
require_once 'fun.php';

$param = file_get_contents('notify1.txt');
// $param = file_get_contents('php://input');
file_put_contents('notify.txt', $param);
$param = json_decode($param, true);
$msg = json_decode($param['msg'], true);

$timestamp = $param['timestamp'];
$nonce = $param['nonce'];
$type = $param['type'];
$appid = $msg['appid'];
$cp_orderno = $msg['cp_orderno'];
$cp_extra = $msg['cp_extra'];
$way = $msg['way'];
$channel_no = $msg['channel_no'];
$payment_order_no = $msg['payment_order_no'];
$total_amount = $msg['total_amount'];
$status = $msg['status'];
$item_id = $msg['item_id'];
$seller_uid = $msg['seller_uid'];
$paid_at = $msg['paid_at'];
$order_id = $msg['order_id'];
$url = file_get_contents('notify_url.txt');
// $res = http_post("https://developer.toutiao.com/api/apps/order/v2/push", [
//     "access_token" => getAccessToken(),
//     "app_name" => "douyin",
//     "open_id" => "111",
//     "order_detail" => json_encode([
//         "order_id" => $cp_orderno,
//         "create_time" => time(),  //订单创建时间
//         // "status" => "已支付",
//         // "amount" => 2,
//         // "total_price" =>(float) $total_amount,
//         // "detail_url" => ""
//     ]),
//     "order_status" => 1,
//     "order_type" => 0,
//     "update_time" => time(),


//     // "item_list" => [
//     //     "item_code" => "",
//     //     "img" => "",
//     //     "title" => "",
//     //     "sub_title" => "", //子订单商品介绍副标题
//     //     "amount" => 2,
//     //     "price" => $total_amount
//     //     ,
//     // ]
// ]);

// var_dump($res);
// exit;
if ($msg['status'] == 'SUCCESS') {
    $res = send_post($url, [
        $timestamp,
        $nonce,
        $type,
        $appid,
        $cp_orderno,
        $cp_extra,
        $way,
        $channel_no,
        $payment_order_no,
        $total_amount,
        $status,
        $item_id,
        $seller_uid,
        $paid_at,
        $order_id,
    ]);
    file_put_contents('res.txt', $res);
    return json_encode(['err_no' => 0, 'err_tips' => 'success']);
} else {
    // send_post($url, [
    //     $timestamp,
    //     $nonce,
    //     $type,
    //     $appid,
    //     $cp_orderno,
    //     $cp_extra,
    //     $way,
    //     $channel_no,
    //     $payment_order_no,
    //     // $total_amount,
    //     $status,
    //     $item_id,
    //     $seller_uid,
    //     $paid_at,
    //     $order_id,
    // ]);


    return json_encode(['err_no' => 400, 'err_tips' => 'business fail']);
}

function getAccessToken()
{

    //检测文件是否存在
    $file = 'access_token.txt';
    if (file_exists($file)) {
        //获取文件的最后修改时间
        $mtime = filemtime($file);
        //获取当前时间
        $now = time();
        //判断是否过期
        if (($now - $mtime) < 7000) {
            //未过期，直接读取文件内容
            $access_token = file_get_contents($file);
            return $access_token;
        }
    }

    $access_token = file_put_contents($file, json_decode(http_post("https://developer.toutiao.com/api/apps/v2/token", [
        "appid" => "ttdd96ab2b42a07b6d01",
        "grant_type" => "client_credential",
        "secret" => "028db66bdff3fa44cad6f2fafa1faa3d6571b3b4"
    ]), true)['data']['access_token']);
    return $access_token;


}


function send_post($url, $post_data)
{
    $postdata = http_build_query($post_data);

    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 15 * 60 // 超时时间（单位:s）
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}

